package com.yunjian.admin.config.shiro;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonpLogoutFilter extends LogoutFilter {
	
	private static final Logger logger = LoggerFactory.getLogger(JsonpLogoutFilter.class);
	
	@Override
	protected boolean preHandle(ServletRequest request, ServletResponse response)
			throws Exception {
		Subject subject = getSubject(request, response);
        String redirectUrl = getRedirectUrl(request, response, subject);
        try {
            subject.logout();
        } catch (SessionException ise) {
        	logger.debug("Encountered session exception during logout.  This can generally safely be ignored.", ise);
        }
        // 替换为内部跳转
        WebUtils.toHttp(request).getRequestDispatcher(redirectUrl).forward(request, response);
        return false;
	}
	
}
